home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Graphics 2D / ColorizePict / Source / Colorize.c next >
Encoding:
Text File  |  2000-09-28  |  7.5 KB  |  312 lines  |  [TEXT/CWIE]

  1. // routine to colorize a pixmap.  This puts up a dialog with two colored boxes,
  2. // click in the box to select a foreground a background image to used to colorize
  3. // the picture.
  4. //
  5. // The image colorization is done using copybits, the color selection is done using
  6. // the standard color picker.
  7. //
  8. // Nick Thompson, June '94
  9.  
  10. #include <Dialogs.h>
  11. #include <QDOffscreen.h>
  12. #include <PictUtil.h>
  13. #include <picker.h>
  14. #include <Processes.h>
  15.  
  16.  
  17. void draw_image(CGrafPtr windowToColorize, RGBColor *fg, RGBColor *bg);
  18. pascal Boolean OurFilter(DialogPtr theColorSelectionDialog, EventRecord *event, short *itemHit);
  19. void ColorizeImage(WindowPtr imageToColorize);
  20.  
  21. //------------------------
  22. // items in the color selection dialog
  23.  
  24. enum {
  25.     kColorSelectionDialogOKBtn = 1,
  26.     kColorSelectionDialogCancelBtn,
  27.     kColorSelectionDialogInfoIcn,
  28.     kColorSelectionDialogStaticText1,
  29.     kColorSelectionDialogFGColorBox,
  30.     kColorSelectionDialogBGColorBox
  31.     
  32.     // we don't care about the rest at this point
  33. } ;
  34.  
  35. static const RGBColor    kRGBBlack = {0, 0, 0};
  36. static const RGBColor    kRGBWhite = {0xFFFF, 0xFFFF, 0xFFFF};
  37.  
  38. static    RGBColor    theFGColor ;
  39. static    RGBColor    theBGColor ;
  40.  
  41. //-----------------------------------------------------------------------------------------
  42.  
  43. void draw_image(    CGrafPtr windowToColorize, 
  44.                     RGBColor *fg, 
  45.                     RGBColor *bg )
  46. {
  47.     CGrafPtr        savedPort ;
  48.     GDHandle        oldDevice ;
  49.     GWorldPtr        myNewWorld ;
  50.     PixMapHandle    myOffPixMap ;
  51.     
  52.     GetGWorld( &savedPort, &oldDevice ) ;
  53.     
  54.     // get the GWorld from the window refcon
  55.     if((myNewWorld = (GWorldPtr)GetWRefCon ( (GrafPtr)windowToColorize )) == nil)
  56.         return ;
  57.         
  58.     if((myOffPixMap = GetGWorldPixMap( myNewWorld )) == nil )
  59.         return ;
  60.     
  61.     // set up the foreground and background colors so 
  62.     // that we have everything as expecte.  These are 
  63.     // the values passed in.
  64.     
  65.     SetGWorld( myNewWorld, nil ) ;
  66.     RGBForeColor( fg ) ;
  67.     RGBBackColor( bg ) ;
  68.  
  69.     (void) LockPixels( myOffPixMap ) ;
  70.     
  71.     CopyBits( &((GrafPtr)myNewWorld)->portBits,
  72.               &((GrafPtr)myNewWorld)->portBits,
  73.               &((GrafPtr)myNewWorld)->portRect,
  74.               &((GrafPtr)myNewWorld)->portRect,
  75.               srcCopy,
  76.               nil ) ;
  77.               
  78.     (void) UnlockPixels( myOffPixMap ) ;
  79.     
  80.     RGBForeColor( &kRGBBlack ) ;        // ensure the fg and bg colors are 
  81.     RGBBackColor( &kRGBWhite ) ;        // as anticipated
  82.     
  83.     SetGWorld( windowToColorize, nil ) ;
  84.     
  85.     InvalRect( &windowToColorize->portRect ) ;
  86.  
  87.     SetGWorld( savedPort, oldDevice ) ;
  88. }
  89.  
  90.  
  91. pascal Boolean OurFilter(DialogPtr theColorSelectionDialog, EventRecord *event, short *itemHit)
  92. {
  93.  
  94.     ModalFilterUPP         theProc ;
  95.     Boolean                retVal ;
  96.     
  97.     static    Boolean        isDisabled = false ;
  98.     OSErr                theErr = noErr ;
  99.     
  100.     // stuff for getditems etc
  101.     /*Str255                theItem ;*/
  102.     short                iKind;
  103.     Handle                iHandle;
  104.     Rect                iRect;
  105.     Pattern                black;
  106.     
  107.     
  108.     // get the std filter proc, again this is system 7 specific
  109.     // we are usingthe standard filter proc to render the default button.
  110.     // in a real app we'd at least need to handle update events in the filter
  111.     // proc, in order to redraw the user item, however I'm not doing
  112.     // that here because I am very lazy.  You should if you use this approach.
  113.     
  114.     theErr = GetStdFilterProc( &theProc ) ;
  115.     
  116.     if( theErr != noErr )
  117.         ExitToShell() ;
  118.         
  119.     // try to call the standard filter, if it handles the event, we don't
  120.     if( !(retVal = CallModalFilterProc(theProc, theColorSelectionDialog, event, itemHit)) )
  121.     {
  122.         switch (event->what) {
  123.     
  124.             case nullEvent:
  125.                 break;
  126.     
  127.             case keyDown:
  128.             case autoKey:
  129.                 retVal = false;
  130.                 break ;
  131.     
  132.             case updateEvt:
  133.                 // set up the foreground color
  134.                 GetDialogItem ( theColorSelectionDialog, kColorSelectionDialogFGColorBox, &iKind, &iHandle, &iRect) ;
  135.             
  136.                 RGBForeColor ( &theFGColor );
  137.                 FillRect( &iRect, &black) ;
  138.                 
  139.                 RGBForeColor ( &kRGBBlack );
  140.                 PenSize( 1,1) ;
  141.                 InsetRect( &iRect, -4, -4 ) ;
  142.                 FrameRect( &iRect ) ;
  143.                 PenNormal() ;
  144.  
  145.                 // set up the background color
  146.                 GetDialogItem ( theColorSelectionDialog, kColorSelectionDialogBGColorBox, &iKind, &iHandle, &iRect) ;
  147.  
  148.                 RGBForeColor ( &theBGColor );
  149.                 FillRect( &iRect , &black) ;
  150.                 
  151.                 RGBForeColor ( &kRGBBlack );
  152.                 PenSize( 1,1) ;
  153.                 InsetRect( &iRect, -4, -4 ) ;
  154.                 FrameRect( &iRect ) ;
  155.                 PenNormal() ;
  156.                 
  157.                 retVal = false;
  158.                 break ;
  159.     
  160.             default:
  161.                 retVal = false;
  162.                 break ;
  163.         }
  164.     }
  165.     
  166.     return retVal ;
  167. }
  168.  
  169.  
  170.  
  171.  
  172. void ColorizeImage( WindowPtr imageToColorize )
  173. {
  174.  
  175.     DialogPtr        theColorSelectionDialog ;
  176.     GrafPtr            savedPort ;
  177.     
  178.     short            iKind;
  179.     Handle            iHandle;
  180.     Rect            iRect;
  181.     Pattern            black;
  182.     
  183.     RGBColor        savedFGColor ;
  184.     RGBColor        savedBGColor ;
  185.     
  186.     OSErr            theErr ;
  187.     short            itemHit ;   
  188.     
  189.     Point            where = { 0, 0 } ;
  190.     
  191.     GetPort(&savedPort ) ;
  192.     
  193.     
  194.     // post the all purpose color dialog!! use this to get the fg and bg colors
  195.     // as required by the user
  196.     
  197.     // get the dialog
  198.     theColorSelectionDialog = GetNewDialog ( 129, nil, (WindowPtr)-1 );
  199.  
  200.     SetPort( (GrafPtr)theColorSelectionDialog ) ;
  201.     
  202.     // need to ensure the fg and bg colors are filled with approprite colors.
  203.     // just use the defaults.
  204.      
  205.     GetForeColor( &theFGColor ) ;
  206.     savedFGColor = theFGColor ;
  207.     
  208.     GetBackColor( &theBGColor ) ;
  209.     savedBGColor = theBGColor ;
  210.     
  211.     GetDialogItem ( theColorSelectionDialog, kColorSelectionDialogFGColorBox, &iKind, &iHandle, &iRect) ;
  212.  
  213.     RGBForeColor ( &theFGColor );
  214.     FillRect( &iRect, &black) ;
  215.     
  216.     RGBForeColor ( &kRGBBlack );
  217.     PenSize( 1,1) ;
  218.     InsetRect( &iRect, -4, -4 ) ;
  219.     FrameRect( &iRect ) ;
  220.     PenNormal() ;
  221.     
  222.     GetDialogItem ( theColorSelectionDialog, kColorSelectionDialogBGColorBox, &iKind, &iHandle, &iRect) ;
  223.  
  224.     RGBForeColor ( &theBGColor );
  225.     FillRect( &iRect , &black) ;
  226.     
  227.     RGBForeColor ( &kRGBBlack );
  228.     PenSize( 1,1) ;
  229.     InsetRect( &iRect, -4, -4 ) ;
  230.     FrameRect( &iRect ) ;
  231.     PenNormal() ;
  232.     
  233.     
  234.     // this is the system 7 way of handling the dialog default item,
  235.     // and the cancel button for the dialog.
  236.     //
  237.     // make sure we are on sys 7 before we get here, or highlight the 
  238.     // default item in the regular way and handle key presses for ok and
  239.     // cancel in the filterproc.
  240.     
  241.     if((theErr =  SetDialogDefaultItem(theColorSelectionDialog, kColorSelectionDialogOKBtn)) != noErr )
  242.         ExitToShell() ;
  243.  
  244.     if((theErr =  SetDialogCancelItem(theColorSelectionDialog, kColorSelectionDialogCancelBtn)) != noErr )
  245.         ExitToShell() ;
  246.  
  247.     
  248.     do {
  249.     
  250.         ModalDialog ( NewModalFilterProc(OurFilter), &itemHit );
  251.         
  252.         switch( itemHit ) {
  253.             
  254.             case kColorSelectionDialogFGColorBox:
  255.             
  256.                 if( GetColor( where, "\pEnter the Background color:", &theFGColor, &theFGColor) ) {
  257.                     // set up the foreground color
  258.                     GetDialogItem ( theColorSelectionDialog, kColorSelectionDialogFGColorBox, &iKind, &iHandle, &iRect) ;
  259.                 
  260.                     RGBForeColor ( &theFGColor );
  261.                     FillRect( &iRect, &black) ;
  262.                     
  263.                     RGBForeColor ( &kRGBBlack );
  264.                     PenSize( 1,1) ;
  265.                     InsetRect( &iRect, -4, -4 ) ;
  266.                     FrameRect( &iRect ) ;
  267.                     PenNormal() ;
  268.                 }
  269.                 
  270.                 break ;
  271.                 
  272.             case kColorSelectionDialogBGColorBox:
  273.                 // get the users choice
  274.                 if( GetColor( where, "\pEnter the Background color:", &theBGColor, &theBGColor) ) {
  275.                 
  276.                     // set up the background color
  277.                     GetDialogItem ( theColorSelectionDialog, kColorSelectionDialogBGColorBox, &iKind, &iHandle, &iRect) ;
  278.     
  279.                     RGBForeColor ( &theBGColor );
  280.                     FillRect( &iRect , &black) ;
  281.                     
  282.                     RGBForeColor ( &kRGBBlack );
  283.                     PenSize( 1,1) ;
  284.                     InsetRect( &iRect, -4, -4 ) ;
  285.                     FrameRect( &iRect ) ;
  286.                     PenNormal() ;
  287.                 } 
  288.                 
  289.                 break ;
  290.                 
  291.             default:
  292.                     break ;
  293.                     
  294.         }
  295.         
  296.     } while( itemHit != ok && itemHit != cancel) ;
  297.     DisposeDialog ( theColorSelectionDialog );
  298.     
  299.     
  300.     SetPort( (GrafPtr)imageToColorize);
  301.     if( itemHit == ok )
  302.         draw_image(    (CGrafPtr)imageToColorize, 
  303.                     &theFGColor, 
  304.                     &theBGColor) ;
  305.  
  306.     SetPort( savedPort ) ;
  307.     
  308.  
  309. }
  310.     
  311.     
  312.